using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace SysCommon.Tools
{
    public class ImageOperation
    {
        public static void EncodeWithString(string inputFileName,string outputFileName)
        {
            System.IO.FileStream inFile;
            byte[] binaryData;

            try
            {
                inFile = new System.IO.FileStream(inputFileName,System.IO.FileMode.Open,System.IO.FileAccess.Read);
                binaryData = new Byte[inFile.Length];
                long bytesRead = inFile.Read(binaryData, 0,(int)inFile.Length);
                inFile.Close();
            }
            catch (Exception )
            {
                return;
            }

            //   Convert   the   binary   input   into   Base64   UUEncoded   output.   
            string base64String;
            try
            {
                base64String =System.Convert.ToBase64String(binaryData,0,binaryData.Length);
            }
            catch (System.ArgumentNullException)
            {
                return;
            }

            //   Write   the   UUEncoded   version   to   the   output   file.   
            System.IO.StreamWriter outFile;
            try
            {
                outFile = new System.IO.StreamWriter(outputFileName,false,System.Text.Encoding.ASCII);
                outFile.Write(base64String);
                outFile.Close();
            }
            catch (Exception )
            {

            }
        }

        public static void Base64Decode(string toFile, string base64String)
        {
            FileStream fileStream=null;
            try
            {
                if (System.IO.File.Exists(toFile)) return;
                fileStream = new FileStream(toFile, FileMode.Create);

                byte[] binaryData = new Byte[fileStream.Length];
                long bytesRead = fileStream.Read(binaryData, 0, (int) fileStream.Length);

                byte[] buffer = Convert.FromBase64String(base64String);
                fileStream.Write(buffer, 0, buffer.Length);
                fileStream.Close();
            }
            catch(Exception )
            {
                fileStream.Close();
            }
        }


        public static string Base64Encode(string fromFile)
        {

            FileStream fileStream = File.Open(fromFile, FileMode.Open);
            byte[] buffer = new byte[fileStream.Length];
            fileStream.Read(buffer, 0, buffer.Length);
            fileStream.Close();
            return Convert.ToBase64String(buffer);
        }

        public static void WriteToFile(string outputFileName, string base64String)
        {
            System.IO.StreamWriter outFile=null;
            try
            {
                outFile = new System.IO.StreamWriter(outputFileName, false, System.Text.Encoding.ASCII);
                outFile.Write(base64String);
                outFile.Close();
            }
            catch (Exception)
            {

            }
            outFile.Close();
        }
    }
}
